package com.learn.english.mapper;

import com.learn.english.model.entity.UserMenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author TISNIW
 * @version 1.0
 * @date 2025/6/21 21:00
 * @desc
 */
@Mapper
public interface MenuMapper {
        /**
         * 添加菜单
         */
        @Insert("INSERT INTO user_menu " +
                        "(parent_id, role_id, operator_id, page_id, name, url, icon, type, sort) " +
                        "VALUES (#{parentId}, #{roleId}, #{operatorId}, [1], #{name}, #{url}, #{icon}, #{type}, #{sort})")
        int insert(UserMenu menu);

        /**
         * 根据角色ID,type,pageId,获取菜单列表
         */
        @Select({
                        "<script>",
                        "select * from user_menu where role_id = #{roleId} and status = 1",
                        "<if test='type != null'> and type = #{type} </if>",
                        "<if test='pageId != null'> and json_contains(page_id, cast(concat('[', #{pageId}, ']') as json)) </if>",
                        "</script>"
        })
        List<UserMenu> selectByRoleId(@Param("roleId") int roleId, @Param("type") Integer type,
                        @Param("pageId") Integer pageId);

        /**
         * 根据条件查询菜单列表
         */
        @Select({
                        "<script>",
                        "select * from user_menu where 1=1",
                        "<if test='name != null'> and name like concat('%', #{name}, '%') </if>",
                        "<if test='status != null'> and status = #{status} </if>",
                        "order by sort asc",
                        "</script>",
        })
        List<UserMenu> selectByCondition(@Param("name") String name, @Param("status") Byte status);

        /**
         * 根据ID更新菜单
         */
        @Update({
                        "<script>",
                        "update user_menu set",
                        "<if test='parentId != null'> parent_id = #{parentId}, </if>",
                        "<if test='name != null'> name = #{name}, </if>",
                        "<if test='url != null'> url = #{url}, </if>",
                        "<if test='icon != null'> icon = #{icon}, </if>",
                        "<if test='type != null'> type = #{type}, </if>",
                        "<if test='sort != null'> sort = #{sort}, </if>",
                        "<if test='pageId != null'> page_id = #{pageId}, </if>",
                        "<if test='status != null'> status = #{status}, </if>",
                        "<if test='updateTime != null'> updated_at = #{updateTime} </if>",
                        "where id = #{id}",
                        "</script>",
        })
        int updateById(UserMenu menu);

        /**
         * 统计子菜单数量
         */
        @Select("select count(*) from user_menu where parent_id = #{id}")
        int countChildren(@Param("id") Integer id);

        /**
         * 根据ID删除菜单
         */
        @Delete("delete from user_menu where id = #{id}")
        int deleteById(@Param("id") Integer id);

        /**
         * 批量删除菜单
         */
        @Delete({
                        "<script>",
                        "delete from user_menu where id in",
                        "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
                        "#{id}",
                        "</foreach>",
                        "</script>"
        })
        int deleteBatchIds(@Param("ids") List<Integer> ids);

        /**
         * 根据ID获取菜单
         */
        @Select("select * from user_menu where id = #{id}")
        UserMenu selectById(@Param("id") Integer id);

        /**
         * 获取所有菜单
         */
        @Select("select * from user_menu order by sort asc")
        List<UserMenu> selectAll();
}
